home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8634 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.1 KB  |  81 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: how can an int data type accept char data?
  5. Date: 5 Mar 1996 07:41:58 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4hhnc6INNqlk@keats.ugrad.cs.ubc.ca>
  8. References: <4hg2si$irt@news.azstarnet.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4hg2si$irt@news.azstarnet.com>,
  12. Howard Salmon  <captarm@azstarnet.com> wrote:
  13. >I thought that the int data type could only accept integer data; 
  14. >however, the example listed below (taken from Dave Mark's book "Learn C 
  15. >on the Macintosh") shows an int variable ("done") accepting character 
  16. >data (i.e. "FALSE").  Dave Mark doesnt explain this.  Can anyone help me 
  17. >out? (This is a program is supposed to find the prime number that 
  18. >follows a previous prime number):
  19.  
  20. FALSE is probably a symbolic constant implemented via a pre-processor macro. It
  21. is not declared anywhere, and <stdio.h> does not define such a constant.
  22.  
  23. Are you sure you typed this program in 100% verbatim?
  24.  
  25.  
  26. In any case, integers in C _can_ hold character constants. A character is what
  27. is called an _integral type_. In expressions, small integral types such as bit
  28. fields and characters are automatically converted to type int, if that can be
  29. used to represent all their values, otherwise to unsigned int. Thus, an
  30. expression like this is valid:
  31.  
  32.     int x;
  33.  
  34.     ...
  35.  
  36.     x += 'A';
  37.  
  38. This will add the collating value of the character a to the integer variable x.
  39. (For example, 65 if your compiler's translation character set is ascii).
  40.  
  41. >#include <stdio.h>
  42. >
  43. >main()
  44. >{
  45. >    int        startingPoint, candidate, i;
  46. >    int        done, foundFactor;
  47. >    
  48. >    done = FALSE; 
  49.  
  50. This will yield an error, since FALSE is not declared. C allows implicit
  51. declarations only for funtions. YOu can call a function for example ``foo();''
  52. that you have not declared, and an implicit declaration will be generated by
  53. the compiler, which looks like ``int foo()''.  If you call a function that
  54. doesn't exist anywhere in your program, or libraries and modules linked to it,
  55. the error will typically only be caught at link time, whereby the linker will
  56. typically complain of unresolved references (assuming you are programming C in
  57. an enviroment where the concept of linker is meaningful).
  58.  
  59. However, if you reference any other kind of object that is not declared, you
  60. will get an error. In the above, the compiler will complain that FALSE has not
  61. been declared, that its type is unknown, etc.
  62.  
  63. >
  64. >                {how can a variable declared as an integer
  65. >                    hold character variables?  Why wouldn't
  66. >                    C insist on a char data type? } 
  67.  
  68. A character string requires quotes. If you wanted a character string FALSE, you
  69. would have to write it "FALSE". The type of such a string literal is ``an array
  70. of (length + 1) char'', and it evaluates to a pointer to the first element.
  71. To create one and assign the address of its first element to a pointer, you can
  72. do this:
  73.  
  74.     char *mystring = "FALSE";
  75.  
  76. The type of "FALSE" is truly incompatible with integers, because it is a
  77. pointer to a character. On the other hand, the expression  mystring[3] is an
  78. integral type, the character 'S'.
  79. -- 
  80.  
  81.